package com.xzssmk.leisure.x.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xzssmk.leisure.x.mapper.StoreLargessMapper;
import com.xzssmk.leisure.x.po.SelectModel;
import com.xzssmk.leisure.x.po.StoreLargess;
import com.xzssmk.leisure.x.service.BaseService;
import com.xzssmk.leisure.x.service.StoreLargessService;
import com.xzssmk.leisure.x.util.DataTables;
import com.xzssmk.leisure.x.util.RequestStatusUtil;

@Service
public class StoreLargessServiceImpl extends BaseService implements StoreLargessService {

    @Autowired
    private StoreLargessMapper storeLargessMapper;

    @Override
    public Map<String, Object> add(StoreLargess storeLargess) {

        Map<String, Object> map = new HashMap<>();
        Integer id = storeLargessMapper.insert(storeLargess);
        if (id > 0) {
            map.put("status", RequestStatusUtil.SUCCESS);
        } else {
            map.put("status", RequestStatusUtil.FAILURE);
        }
        return map;
    }

    @Override
    public Map<String, Object> delete(List<Integer> idlist) {

        Map<String, Object> map = new HashMap<>();
        if (storeLargessMapper.deleteByPrimaryKeys(idlist) > 0) {
            map.put("status", RequestStatusUtil.SUCCESS);
        } else {
            map.put("status", RequestStatusUtil.FAILURE);
        }
        return map;
    }

    @Override
    public Map<String, Object> update(StoreLargess storeLargess) {

        Map<String, Object> map = new HashMap<>();
        if (storeLargessMapper.updateByPrimaryKeySelective(storeLargess) > 0) {
            map.put("status", RequestStatusUtil.SUCCESS);
        } else {
            map.put("status", RequestStatusUtil.FAILURE);
        }
        return map;
    }

    @Override
    public DataTables getPageList(DataTables dataTables) {

        PageHelper.startPage(dataTables.getStart(), dataTables.getLength()); // 核心分页代码
        PageHelper.orderBy("modify_time desc");
        // https: // blog.csdn.net/lvbang_lzt/article/details/54375091
        // https: // blog.csdn.net/evane1890/article/details/63262610
        // https: // blog.csdn.net/qq_33315102/article/details/80572566
        if (!StringUtils.isEmpty(dataTables.getColumn())) {
            String column = propertyToColumn(StoreLargessMapper.class, dataTables.getColumn());
            if (!StringUtils.isEmpty(column)) {
                PageHelper.orderBy(column + " " + dataTables.getOrder());
            }
        }
        List<StoreLargess> listStoreLargess = storeLargessMapper.getPageList(dataTables.getSubSQL());
        PageInfo<StoreLargess> pageInfo = new PageInfo<StoreLargess>(listStoreLargess);
        dataTables.setRecordsTotal(pageInfo.getTotal());
        dataTables.setRecordsFiltered(pageInfo.getTotal());
        dataTables.setData(pageInfo.getList() != null ? pageInfo.getList() : new ArrayList<>());

        return dataTables;
    }

    @Override
    public StoreLargess getById(Integer id) {
        return storeLargessMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<SelectModel> getSelectList() {
        return storeLargessMapper.getSelectList();
    }
}
